﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NightWolf.Sonar;

namespace Nuzzle.Database.Models
{
    [SonarTable("characters")]
    public class CharacterModel : SonarRecord
    {
        [SonarField("ID", true)]
        public int ID;

        [SonarField("Owner")]
        public int Owner;

        [SonarField("Nickname")]
        public string Nickname;

        [SonarField("Level")]
        public int Level;

        [SonarField("Experience")]
        public long Experience;

        [SonarField("Skin")]
        public int Skin;

        [SonarField("Scal")]
        public int Scal;

        [SonarField("Gender")]
        public int Gender;

        [SonarField("Breed")]
        public int Breed;

        [SonarField("Colors")]
        public string Colors = "";

        [SonarField("MapID")]
        public int MapID;

        [SonarField("CellID")]
        public int CellID;

        [SonarField("Direction")]
        public int Direction;

        [SonarField("Kamas")]
        public int Kamas;

        [SonarField("LastUse")]
        public int LastUse;

        [SonarField("CurrentLife")]
        public int CurrentLife;

        [SonarField("Life")]
        public int Life;

        [SonarField("Wisdom")]
        public int Wisdom;

        [SonarField("Strenght")]
        public int Strenght;

        [SonarField("Intelligence")]
        public int Intelligence;

        [SonarField("Luck")]
        public int Luck;

        [SonarField("Agility")]
        public int Agility;

        [SonarField("StatsPoints")]
        public int StatsPoints;

        [SonarField("SpellPoints")]
        public int SpellPoints;

        public Managers.BreedManager.BreedData BreedData
        {
            get
            {
                return Managers.BreedManager.GetBreedData(this.Breed);
            }
        }

        public MapModel Map
        {
            get
            {
                return Managers.MapManager.GetMap(this.MapID);
            }
        }

        public List<int> IndexedColor
        {
            get
            {
                var c = new List<int>();
                var data = Colors.Split(',');
                foreach (var cd in data)
                {
                    if (cd != "")
                    {
                        c.Add(int.Parse(cd));
                    }
                }
                return c;
            }
        }

        public Protocol.Types.Game.Look.EntityLook ToEntityLook()
        {
            return new Protocol.Types.Game.Look.EntityLook(1, new List<int>() { this.Skin }, this.IndexedColor,
                    new List<int>() { this.Scal });
        }

        public Protocol.Types.Game.Character.Choice.CharacterBaseInformations ToCharacterBaseInformations()
        {
            var t = new Protocol.Types.Game.Character.Choice.CharacterBaseInformations(
                (byte)this.Breed, this.Gender == 0 ? false : true, this.ID, (byte)this.Level, this.Nickname,
                this.ToEntityLook());
            return t;
        }

        public Protocol.Types.Game.Context.Roleplay.GameRolePlayCharacterInformations ToGameRolePlayCharacterInformations()
        {
            var t = new Protocol.Types.Game.Context.Roleplay.GameRolePlayCharacterInformations(
                new Protocol.Types.Game.Character.Alignment.ActorAlignmentInformations(0, 0, 0, 0, 0),
                0, 0, 0, "", this.Nickname, this.ID, this.ToEntityLook(),
                new Protocol.Types.Game.Context.EntityDispositionInformations((short)this.CellID, (byte)this.Direction));

            return t;
        }

        public Protocol.Types.Game.Character.Characteristic.CharacterCharacteristicsInformations ToCharacterCharacteristicsInformations()
        {
            //TODO: Build type correctement
            var t = new Protocol.Types.Game.Character.Characteristic.CharacterCharacteristicsInformations(this.Experience, 0, 0,
                this.Kamas, this.StatsPoints, this.SpellPoints,
                new Protocol.Types.Game.Character.Alignment.ActorExtendedAlignmentInformations(0, 0, 0, false, 0, 0, 0, 0, 0),
                this.CurrentLife, 1000, 10000, 10000, 6, 3,
                new Protocol.Types.Game.Character.Characteristic.CharacterBaseCharacteristic(1000, 0, 0, 0),
                new Protocol.Types.Game.Character.Characteristic.CharacterBaseCharacteristic(100, 0, 0, 0),
                new Protocol.Types.Game.Character.Characteristic.CharacterBaseCharacteristic(6, 0, 0, 0),
                new Protocol.Types.Game.Character.Characteristic.CharacterBaseCharacteristic(3, 0, 0, 0),
                new Protocol.Types.Game.Character.Characteristic.CharacterBaseCharacteristic((short)this.Strenght, 0, 0, 0),
                new Protocol.Types.Game.Character.Characteristic.CharacterBaseCharacteristic((short)this.Life, 0, 0, 0),
                new Protocol.Types.Game.Character.Characteristic.CharacterBaseCharacteristic((short)this.Wisdom, 0, 0, 0),
                new Protocol.Types.Game.Character.Characteristic.CharacterBaseCharacteristic((short)this.Luck, 0, 0, 0),
                new Protocol.Types.Game.Character.Characteristic.CharacterBaseCharacteristic((short)this.Agility, 0, 0, 0),
                new Protocol.Types.Game.Character.Characteristic.CharacterBaseCharacteristic((short)this.Intelligence, 0, 0, 0));

            return t;
        }
    }
}
